package cn.dida.service.impl;

import cn.dida.dao.DataStatisticsDao;
import cn.dida.dao.ProductDao;
import cn.dida.model.Pageable;
import cn.dida.model.Product;
import cn.dida.model.ScanStatics;
import cn.dida.model.stastic.ProductScanCountByDate;
import cn.dida.service.DataStatisticsService;
import cn.dida.util.KeyValueForDate;
import cn.dida.util.SplitDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhangzhiling on 2017/8/11.
 */
@Service
public class DataStatisticsServiceImpl implements DataStatisticsService {

    @Autowired
    private DataStatisticsDao dataStaDao;


    /**
     * 获取产品列表
     * @param companyId
     * @return
     */
    @Override
    public List<Product>  getProList(Integer companyId){
        List<Product> list = dataStaDao.getProList(companyId);

        return list;
    }
    /**
     * 扫描
     * @param productId
     * @param timeStart
     * @param timeEnd
     * @param type
     * @return
     */
    @Override
    public List<Map<String,Object>> dataCountDayMonth(Integer companyId,String productId, Long timeStart, Long timeEnd, Integer type ){

        String startTime=longToStr(timeStart, "yyyyMMdd");
        String endTime = longToStr(timeEnd,"yyyyMMdd");
        List<Map<String,Object>> sqlList = new ArrayList<>();
        if(type == 1){  //按天
            sqlList = dataStaDao.dataCountByDay(companyId,productId,startTime,endTime);

        }else if(type == 2){  //按月

            sqlList = dataStaDao.dataCountByMonth(companyId,productId,startTime,endTime);

        }
        return sqlList;

    }

    /**
     * 用户数据
     * @param productId
     * @param timeStart
     * @param timeEnd
     * @return
     */
    @Override
    public List<Map<String,Object>> dataCountByHour( Integer companyId,String productId, Long timeStart, Long timeEnd) {

        List<Map<String,Object>> sqlList = new ArrayList<>();
        String startTime=longToStr(timeStart, "yyyyMMdd");
        String endTime = longToStr(timeEnd,"yyyyMMdd");

        sqlList = dataStaDao.dataCountByHour(companyId,productId,startTime,endTime);
            


        return sqlList;
    }

    /**
     * 新增用户
     * @param companyId
     * @param productId
     * @param timeStart
     * @param timeEnd
     * @param type
     * @return
     */
    @Override
    public List<Map<String,Object>> newOpenCountByDate( Integer companyId,String productId, Long timeStart, Long timeEnd, Integer type) {

        List<Map<String,Object>> sqlList = new ArrayList<>();
        String startTime=longToStr(timeStart, "yyyyMMdd");
        String endTime = longToStr(timeEnd,"yyyyMMdd");
        if(type == 1){  //按天

            sqlList = dataStaDao.newOpenCountByDay(companyId,productId,startTime,endTime);

        }else if(type == 2){  //按月

            sqlList = dataStaDao.newOpenCountByMonth(companyId,productId,startTime,endTime);

        }
        return sqlList;
    }

    /**
     * 获取月/日统计列表数据
     * @param companyId
     * @param productId
     * @param timeLimitStart
     * @param timeLimitEnd
     * @param type 1.按天；2.按月
     * @param page
     * @return
     */
    @Override
    public Pageable<Map<String,Object>> getStatsDateList(Integer companyId, String productId, Long timeLimitStart,
                                                         Long timeLimitEnd,Integer type,Integer page){

        Pageable<Map<String,Object>> list=null ;
        String startTime=longToStr(timeLimitStart, "yyyyMMdd");
        String endTime = longToStr(timeLimitEnd,"yyyyMMdd");
        if(type == 1){
            list=dataStaDao.getStatsDayList(companyId, productId, startTime,endTime,page);
        }else if(type == 2){
            list=dataStaDao.getStatsMonthList(companyId, productId, startTime,endTime,page);
        }
        return list;
    }

    private String longToStr(Long date, String formate){

        Date d1=new Date(date);
        String time  = new SimpleDateFormat(formate).format(date);// 定义起始日期
        return time;
    }

    /**
     * 获取月/日统计列表 导出数据
     * @param companyId
     * @param productId
     * @param timeLimitStart
     * @param timeLimitEnd
     * @param type 1.按天；2.按月
     * @return
     */
    public List<Map<String,Object>> getStatsDateListExport(Integer companyId, String productId, Long timeLimitStart,
                                                               Long timeLimitEnd,Integer type){

        List<Map<String,Object>> list = new ArrayList<>();
        String startTime=longToStr(timeLimitStart, "yyyyMMdd");
        String endTime = longToStr(timeLimitEnd,"yyyyMMdd");
        if(type == 1){
            list = dataStaDao.getStatsDayListExport(companyId,productId,startTime,endTime);
        }else if(type == 2){
            list = dataStaDao.getStatsMonthListExport(companyId,productId,startTime,endTime);
        }

        return list;
    }

    /**
     * 获取区域分布柱形图数据
     * @param producId
     * @return
     */
    @Override
    public List<Map<String,Object>>  getAreaData(Integer companyId,String producId,Long scanStartTime){
        List<Map<String,Object>> list = dataStaDao.getAreaData(companyId,producId,scanStartTime);
        return list;
    }

    /**
     * 获取区域分布 列表数据
     * @param producId
     * @param page
     * @return
     */
    @Override
    public Pageable<Map<String,Object>>  getAreaDataDetail(Integer companyId,String producId,Integer page,Long scanStartTime){
        Pageable<Map<String,Object>> list = dataStaDao.getAreaDataDetail(companyId,producId,page,scanStartTime);
        return list;
    }

    /**
     * 列表数据导出
     * @param producId
     * @return
     */
    @Override
    public List<Map<String,Object>> getAreaDataDetailExport(Integer companyId,String producId,Long scanStartTime){
        List<Map<String,Object>> list  = dataStaDao.getAreaDataDetailExport(companyId,producId,scanStartTime);
        return list;
    }
    
    /**
     * 时段分布 获取x轴展示数组
     * @return
     */
	@Override
	public List<Map<String, Object>> getxAxisTime() {
		// TODO Auto-generated method stub
		return dataStaDao.getxAxisTime();
	}
	
	/**
     * 时段分布 获取列表数据
     * @return
     */
	@Override
	public Pageable<Map<String, Object>> getlistDataByHour(Integer companyId, String productId, Long timeLimitStart,
			Long timeLimitEnd,Integer page) {
		// TODO Auto-generated method stub
		String startTime=longToStr(timeLimitStart, "yyyyMMdd");
        String endTime = longToStr(timeLimitEnd,"yyyyMMdd");
		return dataStaDao.getlistDataByHour(companyId, productId, startTime, endTime,page);
	}
	
	/**
     * 时段分布 列表数据导出
     * @return
     */
	@Override
	public List<Map<String, Object>> getlistDataByHourExport(Integer companyId, String productId, Long timeLimitStart,
			Long timeLimitEnd) {
		// TODO Auto-generated method stub
		String startTime=longToStr(timeLimitStart, "yyyyMMdd");
        String endTime = longToStr(timeLimitEnd,"yyyyMMdd");
		return dataStaDao.getlistDataByHourExport(companyId, productId, startTime, endTime);
	}

}
